package com.llf.Blog.Controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Blog.Mapper.BlogMapper;
import com.llf.Blog.Pojo.Blog;
import com.llf.Blog.Service.BlogService;
import com.llf.Blog.ServiceImpl.BlogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author machenike
 */
@Controller
@Api(value = "提供获取博客年份信息接口",tags = "归档管理")
public class ArchivesController {
    @Autowired
    private BlogService blogService;
    /**
     * 跳转归档页面  并根据时间排序
     */
    @ApiOperation("归档页面跳转接口")
    @GetMapping("toArchives")
    public String toArchives(Model model){
        List<Blog> allByYear2020 = blogService.getAllByYear(2020);
        List<Blog> allByYear2021 = blogService.getAllByYear(2021);
        model.addAttribute("year2020",allByYear2020);
        model.addAttribute("year2021",allByYear2021);
        return "archives";
    }

    /**
     * 根据年份详细查询  如果返回的信息大于10条则显示分页 否则不显示
     * @param quarry
     * @param model
     * @param pageNum
     * @return
     */
    @ApiOperation("根据博客年份搜索接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页",required = true),
            @ApiImplicitParam(dataType = "string",name = "quarry", value = "年份",required = true)
    })
    @GetMapping("/archivesByYear/{pageNum}")
    public String toArchivesByYear( String quarry, Model model, @PathVariable("pageNum")Integer pageNum){
        Integer value = Integer.valueOf(quarry);
        if (value==null ||quarry.length()<4){
            value=2020;
        }
        if (pageNum==null){
            pageNum=1;
        }

        PageHelper.startPage(pageNum, 10);
        List<Blog> allByYear = blogService.getAllByYear(value);
        if (allByYear.size()>0){
            PageInfo<Blog> pageInfo = new PageInfo<>(allByYear);
            PageHelper.clearPage();
            model.addAttribute("page",pageInfo);
            //要查询的时间 作为参数 传回 方便分页时绑定
            model.addAttribute("time",value);
            return "archivesByQuarry";
        }else {
            return "/error/404";
        }

    }

}
